package mtn.bowen.yan.server.service.impl;

import com.jfoenix.controls.JFXButton;
import com.jfoenix.controls.JFXComboBox;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.CacheHint;
import javafx.scene.control.ContentDisplay;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Priority;
import javafx.scene.layout.VBox;
import javafx.scene.text.Font;
import javafx.scene.text.FontWeight;
import javafx.scene.text.Text;
import javafx.stage.Stage;
import mtn.bowen.yan.server.fx.StageManager;
import mtn.bowen.yan.server.service.JavaFxUiService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import java.io.File;
import java.util.Objects;

/**
 * @author <a href=mailto:yanbowen90@foxmail.com> bowen.yan </a>
 * @version 1.0
 * @description: TODO
 * @date 2021-10-26 11:05 AM
 */
@Component
public class JavaFxUiServiceImpl implements JavaFxUiService {

    private final static Logger LOGGER = LoggerFactory.getLogger(JavaFxUiServiceImpl.class);

    @Override
    public HBox getHBox(double prefWidth, double prefHeight) {
        HBox hBox = new HBox();
        hBox.setAlignment(Pos.CENTER);
        hBox.setPadding(new Insets(13, 20, 13, 20));
        makeStyle(prefWidth, prefHeight, hBox);
        return hBox;
    }

    @Override
    public HBox getHBox(Pos pos, double packing, Insets insets) {
        HBox hBox = new HBox();
        hBox.setAlignment(pos);
        hBox.setSpacing(packing);
        hBox.setPadding(insets);
        hBox.setStyle("-fx-border-width: 1px;" + "-fx-border-color: -separator-color;");
        return hBox;
    }

    @Override
    public void makeStyle(double prefWidth, double prefHeight, HBox hBox) {
        hBox.setPrefWidth(prefWidth);
        hBox.setPrefHeight(prefHeight);
        hBox.setStyle("-fx-border-width: 1px;" + "-fx-border-color: -separator-color;");
    }

    @Override
    public HBox getTabHBox(Pos pos) {
        HBox hBox = new HBox();
        hBox.setAlignment(pos);
        hBox.setSpacing(60.0);
        hBox.setPadding(new Insets(10, 10, 10, 10));
        hBox.setStyle("-fx-border-width: 1px;" + "-fx-border-color: -separator-color;");
        return hBox;
    }

    @Override
    public VBox getVBox(Insets insets) {
        VBox vBox = new VBox();
        vBox.setAlignment(Pos.CENTER);
        vBox.setPadding(insets);
        VBox.setVgrow(vBox, Priority.ALWAYS);
        vBox.setSpacing(2.0);
        return vBox;
    }

    @Override
    public JFXButton getButton(String text, String color) {
        JFXButton button = new JFXButton(text);
        button.prefHeight(600.0);
        button.prefWidth(300.0);
        button.setContentDisplay(ContentDisplay.CENTER);
        button.setCacheHint(CacheHint.SPEED);
        button.setStyle("-fx-font-family: STKaiTi;" + color + "-fx-border-width: 1px;-fx-font-size: 22;");
        return button;
    }

    @Override
    public Text getText(String msg, Font font) {
        Text text = new Text(msg);
        text.setFont(font);
        return text;
    }

    @Override
    public Text getText(String msg) {
        Text text = new Text(msg);
        text.setFont(Font.font("KaiTi", FontWeight.NORMAL, 24));
        return text;
    }

    @Override
    public JFXComboBox getComboBox(int item) {
        JFXComboBox box = new JFXComboBox();
        box.getItems().addAll(0, 1, 2, 3, 4, 5, 6);
        box.getSelectionModel().select(item);
        return box;
    }

    @Override
    public ImageView getImageView(double width, double height, String path) {
        ImageView view = new ImageView();
        try {
            File file = new File(this.getClass().getResource(path).getPath());
            view.setImage(new Image(file.toURI().toString()));
            view.setFitHeight(height);
            view.setFitWidth(width);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return view;
    }

    @Override
    public void stageClose(String key) {
        Stage stage = StageManager.getStage(key);
        LOGGER.info("stage: {}", stage);
        if (!Objects.isNull(stage)) {
            stage.close();
            LOGGER.info("关闭场景成功!");
        }
    }

    @Override
    public HBox getPictureHBox(Pos pos, Insets insets) {
        HBox hBox = new HBox();
        hBox.setAlignment(pos);
        hBox.setPadding(insets);
        return hBox;
    }
}
